<?php
declare (strict_types=1);

namespace app\mall\service\product;

use app\common\service\BaseService;
use app\mall\logic\product\Product as ProductLogic;
use app\mall\logic\product\Search;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * 商品管理
 */
class Product extends BaseService
{

    /**
     * @Inject()
     * @var ProductLogic
     */
    protected $product;

    /**
     * @Inject()
     * @var Search
     */
    protected $search;

    /**
     * 商品列表
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/12/05
     *
     * @return Response
     * @throws DbException
     */
    public function list(): Response
    {
        // 搜索功能
        $map = [];
        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['category', 'LIKE', '%,' . input('get.category/d') . ',%'];
        }
        if (input('?get.title') && $title = input('get.title/s')) {
            $map[] = ['title', 'LIKE', '%' . $title . '%'];
            $this->search->addSearch($title);
        }
        if (input('?get.recommend') && $recommend = input('get.recommend/d')) {
            $map[] = ['is_recommend', '=', $recommend];
            $map[] = ['mhm_id', '=', request()->mhm_id];
        }

        $data = $this->product->getList($map);

        return static::parseToData($data, 1, '获取成功');
    }

    public function recommend(): Response
    {
        // 搜索功能
        $map = [];
        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['category', 'LIKE', '%,' . input('get.category/d') . ',%'];
        }

        $data = $this->product->getRecommend($map);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     *
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/18
     *
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function hotSearch(): Response
    {
        $data = $this->search->hotSearch();

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * 机构列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-29
     */
    public function detail($id): Response
    {
        $data = $this->product->getDetail($id);

        return static::parseToData($data, 1, '获取成功');
    }

    /**
     * fetchOrder
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/23
     *
     * @return Response
     */
    public function fetchOrder(): Response
    {
        $productId = Request::get('product/d');
        $data      = $this->product->fetchOrder($productId);

        return static::parseToData($data, 1, '获取成功');
    }

    public function submitOrder(): Response
    {
        $param = Request::post();
        $data  = $this->product->submitOrder($param);

        return static::parseToData($data, 1, '提交成功');
    }
}
